package com.five.community.controller;

import com.five.community.bean.Parking;
import com.five.community.bean.Village;
import com.five.community.bean.vo.ParkingVo;
import com.five.community.contant.AppContants;
import com.five.community.service.ParkingService;
import com.five.community.service.VillageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.Date;
import java.util.List;

/*
    车位管理控制器
 */

@Controller
@RequestMapping("/parking")
@SessionAttributes(value= {"vname", "vid"})//向session中存入指定的域属性名称的对象
public class ParkingController {

    @Autowired
    private ParkingService parkingService;
    @Autowired
    private VillageService villageService;

    //显示车位管理列表画面
    @RequestMapping(value = "/listUi")
    public String listUi(String vname,ModelMap map, Integer pageNum){

        System.out.println("vname = " + vname);

        if (pageNum == null) {
            pageNum = 1;
        }

        // 进行查询条件判断
        List<ParkingVo> parkings = null;
        List<ParkingVo> parkingTotal = null;
        try {
            parkingTotal = parkingService.selectAllAndVname();
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.addAttribute("parkingTotal",parkingTotal);
        try {
            //初始化分页插件模型
            PageHelper.startPage(pageNum, 5);
            if(vname == null || "".equals(vname)){

                //查询所有信息获得列表集合
                //调用业务层获得全部车位信息和所属小区名称
                parkings = parkingService.selectAllAndVname();
            }
            else{
                //查询所有信息获得列表集合
                //按照小区名称查询全部车位信息和所属小区名称
                parkings = parkingService.selectParkingAndVnameByVname(vname);
            }
            //分页模型加入数据
            PageInfo<ParkingVo> pageInfo = new PageInfo<>(parkings);
            map.addAttribute("pageInfo", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.addAttribute("parkings",parkings);
        return "parking_list";
    }

    // 显示添加车位画面
    @RequestMapping("/addUi")
    public String addUi(ModelMap map){
        List<Village> villages = null;
        try {
            villages = villageService.selectAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.addAttribute("villages",villages);
        return "parking_add";
    }

    // 添加车位
    @RequestMapping("/add")
    public String add(ParkingVo parkingVo){
        long id1 = Long.parseLong(parkingVo.getVname());
        parkingVo.setCreateDate(new Date());
        try {
            Parking parking = new Parking(parkingVo.getParkingId(),
                    parkingVo.getParkingName(),parkingVo.getCreateDate(),
                    parkingVo.isState(),id1);
            parkingService.insert(parking);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "parking_add";
    }

    // 根据编号删除车位
    @RequestMapping("/delete")
    public String delete(long id){
        try {
            parkingService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/parking/listUi";
    }

    // 显示编辑画面
    @RequestMapping("/editUi")
    public String editUi(long id, ModelMap map){
        try {
            Parking parking = parkingService.selectById(id);
            List<Village> villages = villageService.selectAll();
            map.addAttribute("parking", parking);
            map.addAttribute("villages",villages);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "parking_edit";
    }

    // 编辑
    @RequestMapping("/edit")
    public String edit(ParkingVo parkingVo) {
        long id2 = Long.parseLong(parkingVo.getVname());
        parkingVo.setCreateDate(new Date());
        try {
            Parking parking = new Parking(parkingVo.getId(),parkingVo.getParkingId(),
                    parkingVo.getParkingName(),parkingVo.getCreateDate(),
                    parkingVo.isState(),id2);
            parkingService.updateById(parking);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "parking_edit";
    }

}
